package com.sicheng.datastruct.queue;

@SuppressWarnings("unused")
public class CircleArrayQueue {
    private final int MAX_SIZE;//队列最大容量
    private int front;  //头指针，指向队列首元素位置
    private int rear;   //尾指针,指向队列最后元素的后一个位置
    private final int[] queue;//模拟队列的数组

    public CircleArrayQueue(int MAX_SIZE) {
        this.MAX_SIZE = MAX_SIZE;
        this.queue = new int[MAX_SIZE];
    }

    private boolean isEmpty() {
        return rear == front;
    }

    private boolean isFull() {
        return (rear + 1) % MAX_SIZE == front;
    }

    public int get() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        int value = queue[front];
        front = (front + 1) % MAX_SIZE;
        return value;
    }

    public void add(int e) {
        if (isFull()) {
            System.out.println("队列已满");
            return;
        }
        queue[rear] = e;
        rear = (rear + 1) % MAX_SIZE;
    }

    public int headQueue() {
        return queue[front];
    }

    private int size() {
        return (rear + MAX_SIZE - front) % MAX_SIZE;
    }

    public void display() {
        for (int i = front; i < front + size(); i++) {
            System.out.printf("queue[%d]=%d%n", i % MAX_SIZE, queue[i % MAX_SIZE]);
        }
    }

}
